﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StoreTransport.DAO;
using StoreTransport.Business.Helper;
using StoreTransport.Resources;

namespace StoreTransport.Presentation.Forms {
    public partial class ProductLineListForm : Form {
        int id = 0;
        
        public ProductLineListForm() {
            InitializeComponent();
        }

        private void ProductTypeList_Load(object sender, EventArgs e) {
            _tbxProductLineName.Select();
            LoadProductLines();
        }
        private void LoadProductLines() {
            StoreTransportDataContext context = CommonHelper.GetContext();
            _grdProductLines.AutoGenerateColumns = false;
            _grdProductLines.DataSource = context.ProductLines.Cast<ProductLine>();
        }

        private void _btxEdit_Click(object sender, EventArgs e) {
            if (_grdProductLines.SelectedRows.Count > 0) {
                id = int.Parse(_grdProductLines.SelectedRows[0].Cells[0].Value.ToString());
                EditItem();
            }
        }

        private void EditItem() {
            ProductLine selectedItem = CommonHelper.GetContext().ProductLines.Where(c => c.ID == id).First();
            _tbxProductLineName.Text = selectedItem.ProductLineName;
            _tbxStandardStoreDays.Text = CommonHelper.GetStringNumber(selectedItem.StandardStoreDays);
            _tbxShipmentDays.Text = CommonHelper.GetStringNumber(selectedItem.ShipmentDays);
            _tbxOrderDays.Text = CommonHelper.GetStringNumber(selectedItem.OrderDayNumber);
            _tbxNunberWarningDays.Text = CommonHelper.GetStringNumber(selectedItem.DefaultWarningDays);
            _tbxMaxStoreDays.Text = CommonHelper.GetStringNumber(selectedItem.MaxSellDays);
            _tbxMinStoreDays.Text = CommonHelper.GetStringNumber(selectedItem.MinSellDays);
            _tbxPackage.Text = CommonHelper.GetStringNumber(selectedItem.Package);
            _tbxStableStoreDays.Text = CommonHelper.GetStringNumber(selectedItem.StableSellDays);
            _lblID.Text = selectedItem.ID.ToString();
        }

        private void _btxCreate_Click(object sender, EventArgs e) {
            _tbxProductLineName.Select();
            _lblID.Text = StoreTransportResource.Form_NewID;
            _tbxProductLineName.Text = string.Empty;
            _tbxNunberWarningDays.Text = string.Empty;
            _tbxOrderDays.Text = string.Empty;
            _tbxStandardStoreDays.Text = string.Empty;
            _tbxShipmentDays.Text = string.Empty;
            _tbxMaxStoreDays.Text = string.Empty;
            _tbxMinStoreDays.Text = string.Empty;
            _tbxPackage.Text = string.Empty;
            _tbxStableStoreDays.Text = string.Empty;
            id = 0;
            _lblID.Text = string.Empty;
        }

        private void _btxSave_Click(object sender, EventArgs e) {
            string errorMessage = CommonHelper.CheckRequiredField(_tbxProductLineName.Text, _lblProductLineName.Text);
            errorMessage += CheckNumberField();
            if (!string.IsNullOrEmpty(errorMessage)) {
                MessageBox.Show(errorMessage, StoreTransportResource.ErrorMessage_ErrorMessageTitle);
                return;
            }
            StoreTransportDataContext context = CommonHelper.GetContext();
            ProductLine pl = new ProductLine();
            if (id != 0) {
                var item = context.ProductLines.FirstOrDefault(c => c.ProductLineName.Equals(_tbxProductLineName.Text));
                if (item != null && item.ID != id) {
                    MessageBox.Show(string.Format(StoreTransportResource.Error_Dupplicate_ProductLine,
                        _tbxProductLineName.Text));
                    return;
                }
                pl = context.ProductLines.Where(c => c.ID == id).First();
            } else {
                if (context.ProductLines.FirstOrDefault(c => c.ProductLineName.Equals(_tbxProductLineName.Text)) != null) {
                    MessageBox.Show(string.Format(StoreTransportResource.Error_Dupplicate_ProductLine,
                        _tbxProductLineName.Text));
                    return;
                }
                context.ProductLines.InsertOnSubmit(pl);
                pl.LastUpdatedDate = DateTime.Today;
            }
            pl.ProductLineName = _tbxProductLineName.Text;
            pl.ShipmentDays = int.Parse(_tbxShipmentDays.Text);
            pl.StandardStoreDays = int.Parse(_tbxStandardStoreDays.Text);
            pl.MaxSellDays = int.Parse(_tbxMaxStoreDays.Text);
            pl.MinSellDays = int.Parse(_tbxMinStoreDays.Text);
            pl.StableSellDays = int.Parse(_tbxStableStoreDays.Text);
            pl.Package = int.Parse(_tbxPackage.Text);
            pl.OrderDayNumber = int.Parse(_tbxOrderDays.Text);
            pl.DefaultWarningDays = int.Parse(_tbxNunberWarningDays.Text);
            context.SubmitChanges();
            LoadProductLines();
        }

        private void _btnDelete_Click(object sender, EventArgs e) {
            if (_grdProductLines.SelectedRows.Count > 0) {
                DialogResult result = MessageBox.Show(StoreTransportResource.Message_ConfirmDelete,
                StoreTransportResource.MessageBox_ConfirmDelete, MessageBoxButtons.YesNo);
                if (result == System.Windows.Forms.DialogResult.Yes) {
                    try {
                        StoreTransportDataContext context = CommonHelper.GetContext();
                        foreach (DataGridViewRow row in _grdProductLines.SelectedRows) {
                            id = int.Parse(row.Cells[0].Value.ToString());
                            ProductLine selectedItem = context.ProductLines.Where(c => c.ID == id).First();
                            context.ProductLines.DeleteOnSubmit(selectedItem);
                            context.SubmitChanges();
                        }
                        LoadProductLines();
                        id = 0;
                    } catch (Exception ex) {
                        MessageBox.Show(StoreTransportResource.ErrorMessage_CanNotDeleteItem);
                    }
                }
            } else {
                MessageBox.Show(StoreTransportResource.NoItemSelected);
            }
        }

        private string CheckNumberField() {
            string errorMessage = string.Empty;
            errorMessage += CommonHelper.CheckNumberValue(_tbxStandardStoreDays.Text, _lblStandardStoreDays.Text);
            errorMessage += CommonHelper.CheckNumberValue(_tbxShipmentDays.Text, _lblShipmentDays.Text);
            errorMessage += CommonHelper.CheckNumberValue(_tbxMaxStoreDays.Text, _lblMaxStoreDays.Text);
            errorMessage += CommonHelper.CheckNumberValue(_tbxStableStoreDays.Text, _lblStableStoreDays.Text);
            errorMessage += CommonHelper.CheckNumberValue(_tbxMinStoreDays.Text, _lblMinStoreDays.Text);
            errorMessage += CommonHelper.CheckNumberValue(_tbxPackage.Text, _lblPackage.Text);
            errorMessage += CommonHelper.CheckNumberValue(_tbxOrderDays.Text, _lblOrderDayNumber.Text);
            errorMessage += CommonHelper.CheckNumberValue(_tbxNunberWarningDays.Text, _lblWarningDayNumber.Text);
            return errorMessage;
        }

        private void _grdProductLines_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
            id = int.Parse(_grdProductLines.Rows[e.RowIndex].Cells[0].Value.ToString());
            EditItem();
        }
    }
}
